package cluster

import akka.actor.{Actor, ActorPaths, ActorRef, ActorSelection, ActorSystem, Props}
import akka.cluster.client.ClusterClient.Send
import akka.cluster.client.{ClusterClient, ClusterClientSettings, SubscribeContactPoints}
import com.typesafe.config.ConfigFactory
import spark.woker.SparkWoker.{clientHost, clientPort}
import cluster.Cluster3.sparkWoker

/**
 * @Description: TODO
 * @Author: DJJ
 * @Date: 2019/12/13 13:31
 */
class ClusterWoker(clusterClient: ActorRef) extends Actor{

/*  override def preStart(): Unit = {
    clusterClient ! SubscribeContactPoints
  }*/

  override def receive: Receive = {
    case "start" =>
     println("start")
  }
}
object ClusterWoker extends App{
  /**
   *  客户端向集群发送消息
   */
  val config = ConfigFactory.parseString(
    s"""
       |akka.actor.provider="akka.remote.RemoteActorRefProvider"
       |akka.remote.netty.tcp.hostname="127.0.0.1"
       |akka.remote.netty.tcp.port=8080
        """.stripMargin)

  val sparkWoker = ActorSystem("ClusterWoker", config)
  val path1="akka.tcp://Cluster@127.0.0.1:9990/system/receptionist"
  val path2="akka.tcp://Cluster@127.0.0.1:9991/system/receptionist"
   val initialContacts= Set(ActorPaths.fromString(path1), ActorPaths.fromString(path2))
  private val actorRef: ActorRef = sparkWoker.actorOf(ClusterClient.props(ClusterClientSettings(sparkWoker).withInitialContacts(initialContacts)),"sparkWoker")
  actorRef ! Send("/user/Cluster","master",localAffinity = true)


}
